package router

import (
	"gitee.com/yangguoliang001/go-gin-vue3/common"
	"gitee.com/yangguoliang001/go-gin-vue3/controller/admin"
	"gitee.com/yangguoliang001/go-gin-vue3/controller/authorized"
	"gitee.com/yangguoliang001/go-gin-vue3/controller/dashboard"
	"gitee.com/yangguoliang001/go-gin-vue3/controller/helper"
	"gitee.com/yangguoliang001/go-gin-vue3/controller/mail"
	"gitee.com/yangguoliang001/go-gin-vue3/controller/menu"
	"gitee.com/yangguoliang001/go-gin-vue3/routes/middleware"
	_type "gitee.com/yangguoliang001/go-gin-vue3/type"
)

/*
验证功能：
	签名验证：CheckSignature
	登录验证：CheckLogin
	RBAC权限验证：CheckRBAC
*/

func SetApiRouter(resource *_type.Resource) {
	// 使用拦截器，判断是否初始化数据库
	resource.R.Use(middleware.CheckInstall())

	db := common.InitMysql()
	redisClient := common.InitRedis()
	resource.DB = db
	resource.Cache = redisClient

	adminController := admin.New(resource.Logger, resource.DB, resource.Cache)
	dashboardController := dashboard.New(resource.Logger, resource.DB, resource.Cache)
	mailController := mail.New(resource.Logger, resource.DB, resource.Cache)
	authorizedController := authorized.New(resource.Logger, resource.DB, resource.Cache)
	menuController := menu.New(resource.Logger, resource.DB, resource.Cache)
	helperController := helper.New(resource.Logger, resource.DB, resource.Cache)
	// 拦截器
	interceptor := middleware.New(resource.Logger, resource.DB, resource.Cache)

	// 登录接口
	login := resource.R.Group("/api")
	{
		login.POST("/login", adminController.Login)
		login.GET("/queryAdminByUsername", adminController.QueryAdminByUsername)
	}

	//	登录验证 签名验证 无需 RBAC 验证
	notRBAC := resource.R.Group("/api", middleware.AuthMiddleware())
	//notRBAC := resource.R.Group("/api")
	{
		notRBAC.POST("/logout", adminController.Logout)
		notRBAC.POST("/admin/info", adminController.QueryAdminByUsername)
		notRBAC.GET("/dashboard", dashboardController.QueryDashboardInfo)

		notRBAC.GET("/mail/info", mailController.QueryMailInfo)
		notRBAC.PATCH("/mail/info", mailController.UpdateMailInfo)

	}
	// 登录验证 RBAC 验证
	//api := resource.R.Group("/api") //
	api := resource.R.Group("/api", middleware.AuthMiddleware(), interceptor.RGBAMiddleware()) //
	{
		api.GET("/authorized", authorizedController.QueryAuthList)
		api.POST("/authorized", authorizedController.AddAuthorized)
		api.PATCH("/authorized/used", authorizedController.UpdateAuthorized)
		api.DELETE("/authorized/:id", authorizedController.DeleteAuthorized)

		api.GET("/authorized_api", authorizedController.QueryAuthApiList)
		api.POST("/authorized_api", authorizedController.AddAuthorizedApi)
		api.DELETE("/authorized_api/:id", authorizedController.DeleteAuthorizedApi)

		api.GET("/admin", adminController.QueryAdminList)
		api.POST("/admin", adminController.AddAdmin)
		api.PATCH("/admin/used", adminController.DisabledAdmin)
		//api.PATCH("/admin/offline", adminController.DisabledAdmin)
		api.DELETE("/admin/:id", adminController.DeletedAdmin)
		api.GET("/admin/menu/:id", adminController.QueryAdminMenu)
		api.POST("/admin/menu", adminController.EditAdminMenu)

		api.GET("/menu", menuController.QueryMenuList)
		api.GET("/menu/:id", menuController.QueryMenuItemDetail)
		api.POST("/menu", menuController.EditMenu)
		api.PATCH("/menu/used", menuController.UpdateMenuUsed)
		api.PATCH("/menu/sort", menuController.UpdateMenuSort)
		api.DELETE("/menu/:id", menuController.DeleteMenu)
		api.GET("/menu_action", menuController.QueryMenuActionList)
		api.POST("/menu_action", menuController.AddMenuAction)
		api.DELETE("/menu_action/:id", menuController.DeleteMenuAction)

		api.POST("/helper/cache/search", helperController.SearchCache)
		api.PATCH("/helper/cache/clear", helperController.ClearCache)
		api.GET("/helper/data/dbs", helperController.QueryDbs)
		api.POST("/helper/data/tables", helperController.QueryTables)
		api.POST("/helper/data/mysql", helperController.SearchMysql)
	}
}
